/*
 * Copyright 2006-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package asphalt2.processor
{
	import asphalt2.control.IControl;
	import asphalt2.event.PageContainerEvent;
	import asphalt2.util.UIUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	/**
	 * 画面遷移を伴うプロセッサー
	 * @author c9katayama
	 **/
	public class BasePageTransitionProcessor extends BaseProcessor implements IPageTransitionProcessor
	{
		protected var _nextPageClass:Class;
		
		protected var _pageContainerId:String;
		
		protected var _parameters:Object;
		/**
		 * @param control コントロールクラス
		 * @param nextPageClass 次に表示するページのクラス
		 * @param pageContainerId 表示対象のPageContainerのid nullの場合、自分の親に対して画面入れ替えをします。
		 * @param parameters 次に表示するページに対してセットするプロパティ　このプロパティを使って、画面間のデータを受け渡しできます。
		 */
		public function BasePageTransitionProcessor(control:IControl, 
													nextPageClass:Class, 
													pageContainerId:String=null,
													parameters:Object=null)		
		{
			super(control);
			this._nextPageClass = nextPageClass;
			this._pageContainerId = pageContainerId;
			this._parameters = parameters;
		}
		public function get pageContainerId():String
		{
			return _pageContainerId;
		}		
		public function get nextPageClass():Class
		{
			return _nextPageClass;
		}
		public function get parameters():Object{
			return _parameters;
		}
		//@deprecated
		public function get nextPageProperties():Object{
			return _parameters;
		}
		
		override public function doProcess(event:Event):void{
			doFinalize();
		}
		public function doFinalize():void
		{
			invokeControl("finalize",this,"moveToNextPage");
		}
		public function moveToNextPage():void{
			var targetId:String = findTargetContainerId();
			var e:PageContainerEvent = createPageContainerEvent(targetId);			
			control.UI.dispatchEvent(e);
			dispose();
		}
		protected function findTargetContainerId():String{
			var targetId:String = pageContainerId;
			if(targetId == null){
				var parent:DisplayObjectContainer = control.UI.parent;
				targetId = UIUtil.findRecentPageContainerId(parent);
			}
			return targetId;			
		}
		protected function createPageContainerEvent(targetPageContainerId:String):PageContainerEvent{
			return new PageContainerEvent(targetPageContainerId,nextPageClass,nextPageProperties);
		}
		protected override function dispose():void{
			super.dispose();
			_nextPageClass = null;
			_pageContainerId = null;
			_parameters = null;
		}
	}
}